package org.panxiong.openapi.openapi;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.panxiong.openapi.openapi.model.Components;
import org.panxiong.openapi.openapi.model.Info;
import org.panxiong.openapi.openapi.model.Paths;
import org.panxiong.openapi.openapi.model.Server;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * OpenAPI规范需要的格式
 */
public class OpenApiDocument implements Serializable {

    @JsonProperty("openapi")
    private String openapi = "3.0.0";

    @JsonProperty("info")
    private Info info = new Info();

    @JsonProperty("servers")
    private List<Server> servers = new ArrayList<>();

    @JsonProperty("paths")
    private Paths paths = new Paths();

    @JsonProperty("components")
    private Components components = new Components();

    public OpenApiDocument() {
    }

    public String getOpenapi() {
        return openapi;
    }

    public void setOpenapi(String openapi) {
        this.openapi = openapi;
    }

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }

    public List<Server> getServers() {
        return servers;
    }

    public void setServers(List<Server> servers) {
        this.servers = servers;
    }

    public Paths getPaths() {
        return paths;
    }

    public void setPaths(Paths paths) {
        this.paths = paths;
    }

    public Components getComponents() {
        return components;
    }

    public void setComponents(Components components) {
        this.components = components;
    }

    // 用于序列化和打印 JSON
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        OpenApiDocument document = new OpenApiDocument();
        document.info.setTitle("Swagger Petstore");
        document.info.setDescription("This is a sample server for a pet store.");
        document.info.setVersion("1.0.0");
        document.servers.add(new Server("https://api.example.com/v1", "The main API server"));
        document.paths.addPathItem("/pets", new Paths.PathItem());
        // document.components.addSchema("Pet", new PetSchema());

        String json = mapper.writeValueAsString(document);
        System.out.println(json);
    }

}
